package com.lbb.simple.demo.adapter

import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.lbb.simple.R
import com.lbb.simple.databinding.ItemFmRecommendBinding
import com.lbb.simple.demo.bean.Fm
import com.lbb.utils.ActivityCollectorUtil
import com.lbb.utils.DisplayUtil
import com.lbb.utils.ThemeUtil
import com.lbb.utils.recycler.BaseHolder
import com.lbb.utils.recycler.BaseStateAdapter
import com.lbb.utils.recycler.BaseViewHolder
import jp.wasabeef.glide.transformations.RoundedCornersTransformation

class FmRecommendAdapter : BaseStateAdapter<BaseHolder<ItemFmRecommendBinding>>() {
    var list= mutableListOf<Fm>()
        set(value) {
            if (value.isNullOrEmpty()) {
                state = TYPE_EMPTY
            } else {
                field = value
                state = TYPE_CONTENT
            }
        }
    companion object {
        const val TYPE_TOP = 1
        const val TYPE_BOTTOM = 2
    }
    override fun getItemSize(): Int {
        return  list.size+2
    }

    override fun getItemType(position: Int): Int {
      //  return super.getItemType(position)
        if(position==0&&state==TYPE_CONTENT){
            return TYPE_TOP
        }else if(getItemSize()-1==position&&state==TYPE_CONTENT){
            return TYPE_BOTTOM
        }
        return super.getItemType(position)
    }
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            when(viewType){
                TYPE_TOP->{
                    val textView=TextView(context)

                    textView.layoutParams= ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
                    textView.gravity=Gravity.START
                  //  textView.setPadding(DisplayUtil.dp2px(10f),DisplayUtil.dp2px(10f),DisplayUtil.dp2px(10f),DisplayUtil.dp2px(10f))
                    textView.text="听FM"
                    textView.textSize = 16f
                    val activity = ActivityCollectorUtil.getLastActivity()
                    activity?.let {
                        textView.setTextColor(ThemeUtil.getColor(it, R.attr.textColor))
                    }
                    return BaseViewHolder(textView)
                }
                TYPE_BOTTOM->{
                    val textView=TextView(context)

                    textView.layoutParams= ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
                    textView.gravity=Gravity.CENTER
               //     textView.setPadding(DisplayUtil.dp2px(10f),DisplayUtil.dp2px(10f),DisplayUtil.dp2px(10f),DisplayUtil.dp2px(10f))
                    textView.text="查看更多"
                    textView.textSize = 16f
                    val activity = ActivityCollectorUtil.getLastActivity()
                    activity?.let {
                        textView.setTextColor(ThemeUtil.getColor(it, R.attr.textColor))
                    }
                    return BaseViewHolder(textView)
                }
                else->{
                    return BaseHolder(ItemFmRecommendBinding.inflate(
                        LayoutInflater.from(context),parent,false))
                }
            }
    }

    override fun onBindHolder(holder: BaseViewHolder, position: Int) {
        if(holder is BaseHolder<*>){
            holder as BaseHolder<ItemFmRecommendBinding>
            val p=position-1
            Glide.with(context).load(list[p].coverUrl).apply(
                RequestOptions.bitmapTransform(
                    RoundedCornersTransformation(
                        DisplayUtil.dp2px(6f),
                        0,
                        RoundedCornersTransformation.CornerType.ALL
                    )
                )
            ).into(holder.binding.imageView1)
            holder.binding.textView1.text = list[p].radioName
            holder.binding.textView2.text = list[p].author
            holder.binding.textView3.text = list[p].viewCount.toString()
        }
    }


}